{
    <-- Catherby Lobster Fisher/Cooker w/ Forms v1.3-->
         by IMPerfection
          - MapFix by Phalanx

Fishes lobsters at Catherby fishing spot     (1.0)
Moves to prevent logouts while fishing       (1.0)
Optionally cooks lobsters                    (1.2)
Deposits cooked lobsters at Catherby bank    (1.0)
Drops burnt lobsters                         (1.1)
Sleeps whenever nescessary                   (1.0)
Optional ProcessMapNoCompass                 (1.2)
  - Dont use unless you're damn lazy,
    seems less accurate to me
Forms!                                       (1.3)

Directions:
 - Turn map to perfect cross with North at top
   - Or get near-perfect and use other MapMode
 - Start in the Catherby bank
 - Ensure you have cooked lobs on visible bank page
 - Bring only sleeping bag and lobster pot
}
program CatherbyFisher;
const
 SDelay=30;

var
  x, y, loops, logins, delay: Integer;
  l1, l2, l3, l4, l5: Integer;
  Lobster, RawLobster: Integer;
  username, password: String;
  form, self: TForm;
  Application: TApplication;
  PmLabel, UserLabel, PassLabel, MapLabel: TLabel;
  txtUser, txtPass: TEdit;
  ButtonStart: TButton;
  Cook, MapM: TComboBox;
  DoWhat,MP: Integer;
  start: Boolean;

procedure LogInIfNeeded; export;
begin
if(logins < 21)then
begin
  if(LoginScreen)then
    begin
      Writeln('Clearing username, password fields');
      ClickMouse(400,280,True);
      Wait(2000);
    end;
  if(StartScreen)then
  begin
    Writeln('Choosing existing user');
    ClickMouse(340,280,True);
    loops:=0;
      repeat
        Wait(200);
        loops:=loops+1;
      until(LoginScreen)or (loops>=30);
  end;
  if(GetColor(338,125)=5524540)and //p2p login
    (GetColor(87,145)=3472636)then //p2p login
  //if(LoginScreen)then              //f2p login
  begin
    Writeln('Trying to log in');
    //ClickMouse(100,260,True);      //f2plogin
    ClickMouse(260,290,True);      //p2plogin
    Wait(500);
    SendKeysSilent(username);
    Wait(2000);
    ClickMouse(120,310,True);
    Wait(500);
    SendKeysSilent(password);
    Wait(2000);
      repeat
        ClickMouse(380,250,True);
        logins:= logins + 1;
        Wait(1000);
      until(not loginscreen);
    Wait(1000 + random(20));
  end;
  if(GetColor(56,117)=16777215)and
    (GetColor(57,118)=0)and
    (GetColor(455,216)=16777215)and
    (GetColor(454,215)=0)then
    begin
      Writeln('Closing Welcome box');
        if(IsTextAt(145, 220, 'No that wasn t me - Cancel the request!'))then
          begin
            Clickmouse (250,245,True);
          end else
            ClickMouse(250,230,True);
        Wait(2000 + random(20));
        //not sure what these do, they were in kaitnieks's login procedure
        if(GetColor(169,134)=65535)and
          (GetColor(441,130)=0) and
          (GetColor(91,198)=0)then
          begin
            ClickMouse(250,207,True);
            Wait(2000 + random(20));
          end;
        if(GetColor(139,76)=33023) and
          (GetColor(385,280)=65535)then
          begin
            ClickMouse(250,314,True);
            Wait(2000 + random(20));
          end;
    end;
end;
SetTimeOut(20,'LOGINIFNEEDED');
end;


Procedure FixMap(Path:Integer);
var
 bool:extended;
 x,y:integer;
Begin
 bool:=0;
 repeat
  MoveMouse(1+random(3),1+random(3));
  Wait(10);
  MoveMouse(465+random(3),20+random(3));
  Wait(Delay);
  if(MP = 1)then
  begin
    if ProcessMap=False then Delay:=Delay+5
    else ProcessPath(Path, x, y, bool, 0.1);
  end else
  if(MP = 2)then
  begin
    if ProcessMapNoCompass=False then Delay:=Delay+5
    else ProcessPath(Path, x, y, bool, 0.1);
  end;
  if (Delay>80) then
  begin
   Repeat
    Wait(1000);
   Until (LogInScreen) or (StartScreen) or ((GetColor(289,290)<>0)and(GetColor(93,399)<>9203557)); {members start screen}
   Delay:=SDelay;
   LogInIfNeeded;
  end;
 until bool=1;
end;
//Going by the landmarks included in the path to get to a destination - originally made by kait, modified by me.
procedure GoByPath(path: Integer);
var
 HowFar: Integer;
 x, y: Integer;
 r: Extended;
 Dest: Boolean;
 NR:integer;
 GoodMap: Boolean;
begin
 NR:=0;
 fixmap(Path);
 repeat
  ClickMouse(460+random(3),15+random(3),True);
  Wait(300);
  if(MP = 1)then
    GoodMap:= ProcessMap;
  if(MP = 2)then
    GoodMap:= ProcessMapNoCompass;
  if(GoodMap)then
  begin
    HowFar:= ProcessPath(Path, x, y, r, 0.03);
    if(r > 0.8)then
    begin
     NR:=0;
     if(HowFar = 0) then ClickMap(x, y, True)
     else ClickMap(x + Random(3)-1, y + Random(3) - 1, True);
    end else
  end;
  NR:=NR+1;
  if NR>3 then
  begin
   FixMap(Path);
   NR:=0;
  end else
  begin
   Dest:= (HowFar=0)and(x<=20)and(x>=16)and(y<=22)and(y>=15);
   if(not Dest)then Wait(4000);
  end;
 until(Dest);
 wait(200);
 movemouse(10,10);
 wait(100);
end;

procedure DoSleeping;
begin
  Wait(200 + random(20));
  loops:=0;
    repeat
      loops:=loops+1;
      Wait(200 + random(20))
      ClickMouse (498, 16, True);
      Wait(200 + random(20));
      Movemouse (296, 47);
      Wait(100 + random(20));
    until(IsTextAt(6,2,'Sleeping Bag: sleep')) or (loops>=5);
  ClickMouse(296, 46, True);
  loops:=0;
    repeat
      wait(200 + random(20));
      loops:=loops+1;
    until(IsTextAt(168, 293, 'click here')) or (loops>=25);
  loops:=0;
    repeat
      loops:=loops+1;
      Wait(20 + random(3));
        if(GetColor(209,252)=255)then
          begin
            //Writeln('Resetting wait...');
            loops:=0;
            Wait(2000 + random(20));
          end;
         //get new sw word if no word comes in 30 seconds
        if(loops=1700)then
          begin
            loops:=0;
            ClickMouse (200, 300, True);
            Writeln('Trying again for sw word');
          end;
    until(not IsTextAt(168, 293, 'click here'));
  MoveMouse(10,10);
  Wait(100 + random(20));
end;

procedure DoFishing;
var x, y, tx, ty: Integer;
    f: Extended;
    StartTime: Integer;
    InitX, InitY: Integer;
    FishPath: Integer;
    sleep: Boolean;
begin
  Status('Start fishing');
  StartTime:= GetSystemTime;
  //Initial clicking coordinates
  InitX:= 270;
  InitY:= 265;
  x:= InitX;
  y:= InitY;
  Movemouse(10,10);
  repeat
    f:= 3;
    x:= x - 1 + Random(3);
    y:= y - 1 + Random(3);
    FindColorSpiral(x,y,16578808,x-50,y-50,x+50,y+50);
    FindColorSpiral(x,y,16025680,x-50,y-50,x+50,y+50);
    FindColorSpiral(x,y,10393752,x-50,y-50,x+50,y+50);
    FindColorSpiral(x,y,13410440,x-50,y-50,x+50,y+50);
    FindColorSpiral(x,y,5976092,x-50,y-50,x+50,y+50);
    FindColorSpiral(x,y,12017720,x-50,y-50,x+50,y+50);
    //SpiralFindObj(x,y, -1, x-100, y-100, x+100, y+100, 4, 'fish: Harpoon', MyWait, 60000);
    //if we are fishing 4 minutes or longer, wal a bit left and back
    if(GetSystemTime - StartTime > 240000)then
    begin
      Writeln('Moving');
      GoByPath(FishPath);
      Wait(2000 + random(50));
      MoveMouse(10,10);
      Wait(200);
      StartTime:= GetSystemTime;
    end else
    wait(100);
     MoveMouse(x,y);
     if(IsTextAt(6,2,'fish: Net'))then
     begin
     repeat
     wait(200);
     f:= 3;
     x:= 250 + Random(250);
     y:= 165 - Random(135);
     FindColorSpiral(x,y,16578808,x-50,y-50,x+50,y+50);
     FindColorSpiral(x,y,16025680,x-50,y-50,x+50,y+50);
     FindColorSpiral(x,y,10393752,x-50,y-50,x+50,y+50);
     FindColorSpiral(x,y,13410440,x-50,y-50,x+50,y+50);
     FindColorSpiral(x,y,5976092,x-50,y-50,x+50,y+50);
     FindColorSpiral(x,y,12017720,x-50,y-00,x+50,y+50);
     MoveMouse(x, y);
     wait(100);
     until(IsTextAt(6,2,'fish: Harpoon'));
     end
     if(IsTextAt(6,2,'fish: Harpoon'))then
     begin
      ClickMouse(x, y, False);
      Wait(300 + random(50));
      tx:= x - 49;
      ty:= y + 38;
      if(IsTextInArea(tx-10,ty-15,tx+10,ty+15,tx,ty,'Cage fish'))then
      begin
        ClickMouse(tx+20, ty + 5, True);
        Wait(600 + Random(1000));

        MoveMouse(495,17);
        Wait(300 + random(50));
        MoveMouse(480,220);
        Wait(400 + random(50));
        if(IsTextAt(6,2,'Raw Lobster'))then
        begin;
          Break;
        end;
        Wait(200);
        MoveMouse(10,10);
        Wait(200);
      end else
      begin
        ClickMouse(x, y, True);
        Wait(200);
      end;
    end;
    sleep:= IsTextAt(7, 304, 'You are too tired');
    if(sleep)then
      begin;
        Wait(1000 + random(100));
        DoSleeping;
      end;
  until(false);
end;

procedure DoCooking;
var x,y: Integer;
    first, sleep: Boolean;
begin
x:=315;
y:=100;
  repeat
    MoveMouse(490,15);
    Wait(500);
    MoveMouse(386,53);
    Wait(500);
    if(IsTextAt(6,2,'Raw Lobster: U'))or(IsTextAt(6,2,'Use Raw Lobster with'))then
    begin
      ClickMouse(390+Random(3),50+Random(3),True);
      Wait(200 + random(50));
      MoveMouse(100,100);
      Wait(200 + random(50));
      first:= True;
      repeat
        FindColorSpiral(x,y,1250067,x-100,y-100,x+100,y+100);
        FindColorSpiral(x,y,65793,x-100,y-100,x+100,y+100);
        FindColorSpiral(x,y,1644825,x-100,y-100,x+100,y+100);
        Wait(20);
        MoveMouse(x,y);
        Wait(100);
      until(IsTextAt(6,2,'Use Raw Lobster with Range'))or(IsTextAt(6,2,'Range'));
      ClickMouse(x,y,True);
      Wait(3000 + random(50));
      sleep:= IsTextAt(7, 304, 'You are too tired');
      if(sleep)then
      begin;
        Wait(1000 + random(100));
        DoSleeping;
      end;
    end else
    begin
      Break;
    end;
  until(False);
end;

//Find a banker and deposit all lobs
procedure DoBanking;
begin
  Wait(200);
    repeat
      repeat
      repeat
        x:= Random(500);
        y:= Random(340);
        FindColorSpiral(x,y,10661355,x-50,y-50,x+50,y+50);
        MoveMouse(x, y);
        Wait(200);
      until(IsTextAt(6,2,'Banker: Talk'))or(IsTextAt(6,0,'l d like to access my bank account please'));
      ClickMouse(x, y, True);
      Wait(4000);
    until(IsTextAt(6,0,'l d like to access my bank account please'));
    ClickMouse(17,8,True);
    Wait(6000);
  until(FindBitmap(Lobster,x,y));
  ClickMouse(x,y,True);
  Wait(1000);
  while(IsTextAt(334,264,'25'))do
  begin
    ClickMouse(335,270,True);
    Wait(300);
  end;
  while(IsTextAt(304,264,'Five'))do
  begin
    ClickMouse(305,270,True);
    Wait(300);
  end;
   while(IsTextAt(274,264,'One'))do
  begin
    ClickMouse(275,270,True);
    Wait(300);
  end;
  //Close bank window
  ClickMouse(410,5,True);
  Wait(400);
end;

//Find a banker and deposit all raw lobs
procedure DoBankingRaw;
begin
  Wait(200);
    repeat
      repeat
      repeat
        x:= Random(500);
        y:= Random(340);
        FindColorSpiral(x,y,10661355,x-50,y-50,x+50,y+50);
        MoveMouse(x, y);
        Wait(200);
      until(IsTextAt(6,2,'Banker: Talk'))or(IsTextAt(6,0,'l d like to access my bank account please'));
      ClickMouse(x, y, True);
      Wait(4000);
    until(IsTextAt(6,0,'l d like to access my bank account please'));
    ClickMouse(17,8,True);
    Wait(6000);
  until(FindBitmap(RawLobster,x,y));
  ClickMouse(x,y,True);
  Wait(1000);
  while(IsTextAt(334,264,'25'))do
  begin
    ClickMouse(335,270,True);
    Wait(300);
  end;
  while(IsTextAt(304,264,'Five'))do
  begin
    ClickMouse(305,270,True);
    Wait(300);
  end;
   while(IsTextAt(274,264,'One'))do
  begin
    ClickMouse(275,270,True);
    Wait(300);
  end;
  //Close bank window
  ClickMouse(410,5,True);
  Wait(400);
end;

procedure DropBurnt;
begin
    clickmouse(495,17,true);
    wait(100);
    movemouse(385,49);
    wait(100)
    if(IsTextAt(6,2,'Burnt'))then
    begin
      repeat
      movemouse(495,17);
      wait(100);
      movemouse(385,49);
      Wait(100);
      if(not IsTextAt(6,2,'Burnt'))then
      begin
        movemouse(495,17);
        wait(200);
        movemouse(385,49);
        Wait(200);
        if(not IsTextAt(6,2,'Burnt'))then
        begin
          Break;
        end;
      end;
      ClickMouse(380,55,false);
      wait(200 + random (10));
      ClickMouse(330,85,true);
      Wait(150 + random(10));
      MoveMouse(2,2);
      until(false);
    end;
end;

procedure Startbuttonclick(sender: TObject);
begin
  username:= TxtUser.Text;
  password:= TxtPass.Text;
  Case MapM.Text of
    'Normal':  begin
                  MP:=1;
                end;
    'No Compass':     begin
                  MP:=2;
                end;
    end;
  Case Cook.Text of
    'Cooking':      begin
                  DoWhat:=1;
                end;
    'No Cooking':       begin
                  DoWhat:=2;
                end;
    end;
  start:=true;;
end;

var
  FishPath: Integer;
  CookPath: Integer;
  BankPath: Integer;
  BankPathRaw: Integer;

begin

   Application:= GetApplication;
  Self:= GetSelf;

  Form:= TForm.Create(nil);
  Form.Width := 360;
  Form.Height := 135;
  Form.Position := poScreenCenter;
  Form.BorderStyle := bsDialog;
  Form.Color:=2763306;
  Form.Caption := 'Catherby Lobster Fisher/Cooker by IMPerfection';

  UserLabel := TLabel.Create(Form);
  UserLabel.Top := 12;
  UserLabel.Left := 8;
  UserLabel.Font.Color:= 15306513;
  UserLabel.Caption := 'Username:';
  UserLabel.Parent := Form;

  PassLabel := TLabel.Create(Form);
  PassLabel.Top := 42;
  PassLabel.Left := 8;
  PassLabel.Font.Color:= 15306513;
  PassLabel.Caption := 'Password:';
  PassLabel.Parent := Form;

  PmLabel := TLabel.Create(Form);
  PmLabel.Top := 42;
  PmLabel.Left := 180;
  PmLabel.Font.Color:= 15306513;
  PmLabel.Caption := 'Cook Lobs:';
  PmLabel.Parent := Form;

  MapLabel := TLabel.Create(Form);
  MapLabel.Top := 12;
  MapLabel.Left := 165;
  MapLabel.Font.Color:= 15306513;
  MapLabel.Caption := 'Map Processing:';
  MapLabel.Parent := Form;

  txtUser := TEdit.Create(Form);
  txtUser.Top := 10;
  txtUser.Left := 65;
  txtUser.Width := 90;
  txtUser.Parent := Form;

  txtPass := TEdit.Create(Form);
  txtPass.Top := 40;
  txtPass.Left := 65;
  txtPass.Width := 90;
  txtPass.font.name:='WingDings';
  txtPass.PasswordChar:= 'l';
  txtPass.Parent := Form;

  MapM:= TcomboBox.create(form);
  MapM.Top := 10;
  MapM.Left := 250;
  MapM.Width := 90;
  MapM.Height := 24;
  MapM.parent := Form;
  MapM.DropDownCount:= 2;
  MapM.Items.Add('Normal');
  MapM.Items.Add('No Compass');
  MapM.Sorted:=true;
  MapM.itemindex:= 0;

  Cook:= TcomboBox.create(form);
  Cook.Top := 40;
  Cook.Left := 250;
  Cook.Width := 90;
  Cook.Height := 24;
  Cook.parent := Form;
  Cook.DropDownCount:= 2;
  Cook.Items.Add('Cooking');
  Cook.Items.Add('No Cooking');
  Cook.itemindex:= 0;

  ButtonStart:= TButton.Create(Form);
  ButtonStart.Left := 145;
  ButtonStart.Top := 70;
  ButtonStart.Width := 80;
  ButtonStart.Height := 24;
  ButtonStart.Caption := '&Start';

 // Ore.Parent := form;

  ButtonStart.OnClick := @Startbuttonclick;
  ButtonStart.Parent := Form;
  ButtonStart.Default := True;
  ButtonStart.ModalResult := mrOk;

  Form.ShowModal;
  Form.Free;

  Delay:=SDelay;

  RawLobster := BitmapFromString(12, 7,
       'FC0000B80000CE0000E60000E60000E60000A30000CE0000E60000' +
       'E60000B8000045372CA30000B80000CE0000CE0000CE0000E60000' +
       'CE0000A30000CE0000CE000045372C180C07A30000B80000CE0000' +
       'CE0000A30000CE0000E60000CE0000A30000CE0000B80000120000' +
       'B80000B80000CE0000CE0000CE0000A30000CE0000E60000CE0000' +
       '45372CB80000B80000A30000B80000860000CE0000E60000CE0000' +
       'A30000CE0000B80000120000120000B80000A30000CE0000CE0000' +
       'A30000CE0000CE0000A30000B8000045372CA30000120000A30000' +
       '120000CE0000E60000CE0000A30000CE0000B8000045372C120000' +
       'B80000120000120000');
  Lobster:= BitmapFromString(8, 5,
       '9E56009E56009E56009E56009E56009E5600AD5E005C32009E5600' +
       '9E56009E56008E4D009E56008E4D00703D00AD5E009E56009E5600' +
       '7F450045372C7F4500703D008E4D009E56008E4D008E4D0045372C' +
       '180C070D07008E4D009E56009E5600703D008E4D007F45000D0700' +
       '0D07007F45009E56009E5600');
  l1:= CreateLandmark(6, 7, '04000201000104000401000104000101000104000201000204001B010001');
  l2:= CreateLandmark(7, 4, '01000104000901000104000303000101000104000B010001');
  l3:= CreateLandmark(6, 8, '04000201000104000101000104000901000104000201000104001201000104000A010001');
  l4:= CreateLandmark(12, 5, '04000A09000104000109000104000E090001040007090001040013090001040004');
  l5:= CreateLandmark(5, 5, '04000401000104000101000204000C010001040003010001');
  SetTimeOut(20,'LOGINIFNEEDED');
  FishPath:= CreatePath;
  AddLandmarkToPath(FishPath, l3, 7, 13);
  AddLandmarkToPath(FishPath, l2, 14, 6);
  AddLandmarkToPath(FishPath, l1, 15, 9);
  CookPath:= CreatePath;
  AddLandmarkToPath(CookPath, l5, 3, 2);
  AddLandmarkToPath(CookPath, l2, -7, -7);
  AddLandmarkToPath(CookPath, l4, -13, -11);
  BankPath:= CreatePath;
  AddLandmarkToPath(BankPath, l5, -6, 12);
  BankPathRaw:= CreatePath;
  AddLandmarkToPath(BankPathRaw, l1, 2, 6);
  AddLandmarkToPath(BankPathRaw, l2, -8, 6);
  AddLandmarkToPath(BankPathRaw, l3, -7, 4);

// PWNT! \\
if(start)then
  repeat
    Writeln('Going to fish');
    GoByPath(FishPath);
    Wait(1000 + random(100));
    Writeln('Fishing');
    DoFishing;
    Wait(1000 + random(100));
    if(DoWhat=1)then
    begin
      Writeln('Going to cook');
      GoByPath(CookPath);
      Wait(1000 + random(100));
      Writeln('Cooking');
      DoCooking;
      Wait(1000 + random(100));
    end;
    Writeln('Going to bank');
    if(DoWhat=1)then
    begin
      GoByPath(BankPath);
      Wait(1000 + random(100));
      Writeln('Banking');
      DoBanking;
      Wait(1000 + random(100));
      Writeln('Dropping Burnt Lobsters');
      DropBurnt;
      Wait(1000 + random(100));
    end;
    if(DoWhat=2)then
    begin
      GoByPath(BankPathRaw);
      Wait(1000 + random(100));
      Writeln('Banking');
      DoBankingRaw;
      Wait(1000 + random(100));
    end;
  until(False);
end.